package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Activate(group = {CommonConstants.CONSUMER}) // ,CommonConstants.PROVIDER
public class DubboInvokeFilter implements Filter {

    private static List<Map<String, String>> timeList = new ArrayList<>();
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long startTime = System.currentTimeMillis();
        String methodName = null;
        Map<String, String> timeMap = new HashMap();
        try {
            methodName = invocation.getMethodName();
            // 执行方法
            return invoker.invoke(invocation);
        } finally {
            long endTime = System.currentTimeMillis();
            long responseTime = endTime - startTime;
//            System.out.println(methodName+" invoke time:" + responseTime + "毫秒");
            timeMap.put(methodName,System.currentTimeMillis()+"-"+responseTime);
            timeList.add(timeMap);
        }

    }

    public static List<Map<String, String>> getTimeContainer() {
        return timeList;
    }
}
